package com.yuanjiu.controller.inter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yuanjiu.pojo.yuanjiu.CategoryIdName;
import com.yuanjiu.pojo.yuanjiu.CommentObject;
import com.yuanjiu.pojo.yuanjiu.JiuObject;
import com.yuanjiu.service.yuanjiu.CategoryService;
import com.yuanjiu.service.yuanjiu.HomeService;
import com.yuanjiu.service.yuanjiu.ProductService;
import com.yuanjiu.vo.yuanjiu.PageVo;
import com.yuanjiu.vo.yuanjiu.ProductVo;

@Controller
public class AndroidInterface {
	
	@Autowired
	private HomeService homeService;
	@Autowired
	private ProductService productService;
	@Autowired
	private CategoryService categoryService;
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("homepage")
	public void home(HttpServletRequest request,HttpServletResponse response, HttpSession session, Model model){
		//清除分类数据
		ProductVo productVo = (ProductVo) session.getAttribute("productVo");
		if(productVo != null){
			productVo.setCid(null);
			productVo.setOrder(null);
			productVo.setOrderBy(null);
		}
		
		Map<String, List> map = new HashMap<>();
		
		//获取所有的一级分类
		List<CategoryIdName> categories = homeService.findAllCategories(); 
		map.put("categoryList", categories);
		
		String pageSize ="6";
		//获取热销商品
		List<JiuObject> hotList = homeService.findHotList(pageSize); 
		//处理图片
		for(JiuObject jiuObject : hotList){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		map.put("ListHot",hotList);
		//白酒
		List<JiuObject> listB = homeService.findlatestB(pageSize);
		//处理图片
		for(JiuObject jiuObject : listB){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		map.put("listB", listB);
		//葡萄酒
		List<JiuObject> listPu = homeService.findlatestPu(pageSize);
		//处理图片
		for(JiuObject jiuObject : listPu){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		map.put("listPu", listPu);
		//洋酒
		List<JiuObject> listY = homeService.findlatestY(pageSize);
		//处理图片
		for(JiuObject jiuObject : listY){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		map.put("listY", listY);
		//啤酒
		List<JiuObject> listP = homeService.findlatestP(pageSize);
		//处理图片
		for(JiuObject jiuObject : listP){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		map.put("listP", listP);
		//黄酒|果酒
		List<JiuObject> listH = homeService.findlatestH(pageSize);
		//处理图片
		for(JiuObject jiuObject : listH){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		map.put("listH", listH);
		String callback = request.getParameter("callback");
		ObjectMapper objectMapper = new ObjectMapper();
		try {
			String json = objectMapper.writeValueAsString(map);
			response.setContentType("application/jsonp;charset=utf-8");
			response.getWriter().print(callback+"("+json+")");
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("categoryPage")
	public void goodsList(PageVo voParam, HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model){
		ProductVo vo = (ProductVo) session.getAttribute("productVo");
		if(vo == null){
			vo = new ProductVo();
			session.setAttribute("productVo", vo);
		}
		String cid = request.getParameter("cid");
		if(StringUtils.isNotEmpty(cid)){
			vo.setCid(cid);
		}
		voParam.setPageSize(20);
		
		//设置参数,排序规则在非第一次请求时显示
		if(voParam != null){
			if(StringUtils.isNotEmpty(voParam.getOrder())){
				vo.setOrder(voParam.getOrder());
			}
			if(StringUtils.isNotEmpty(voParam.getOrderBy())){
				vo.setOrderBy(voParam.getOrderBy());
			}
		}
		
		//搜索条件变化，重新设置排序规则
		String reqKeyWord = request.getParameter("keyWord");
		if((StringUtils.isNotEmpty(reqKeyWord)
				&&reqKeyWord.equals( vo.getKeyWord()))
				|| StringUtils.isNotEmpty( request.getParameter("cid"))){
			vo.setOrder(null);
			vo.setOrderBy(null);
		}
		
		vo.setKeyWord(request.getParameter("keyWord"));
		
		//设置分页信息
		if(voParam.getCurrentPage() == null || voParam.getCurrentPage() ==0){
			voParam.setCurrentPage(1);
		}
		if(voParam.getPageSize() == null || voParam.getPageSize() ==0){
			voParam.setPageSize(20);
		}
		
		PageVo page = productService.findProductListByFilter(vo, voParam); 
		//输出
		try {
			response.setContentType("application/jsonp;charset=utf-8");
			PrintWriter printWriter = response.getWriter();
			ObjectMapper objectMapper = new ObjectMapper();
			String result = objectMapper.writeValueAsString(page);
			String callback = request.getParameter("callback");
			printWriter.print(callback+"("+result+")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping("productPage")
	public void single(HttpServletRequest request, HttpServletResponse response, JiuObject jiuObject, Model model){
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		//获取详情
		JiuObject jiuObject2 = productService.getProductById(jiuObject);
		map.put("jiuObject", jiuObject2);
		
		//分类信息
		if(jiuObject2 != null && StringUtils.isNotEmpty(jiuObject2.getCategory())){
			ProductVo productVo = new ProductVo();
			productVo.setCid(jiuObject2.getCategory());
			ProductVo productVo2 = categoryService.getProductVoByCid(productVo);
			map.put("productVo", productVo2);
		}
		
		//获取图片列表
		List<String> imgList = new ArrayList<String>(); 
		if(jiuObject2 != null && StringUtils.isNotEmpty(jiuObject2.getPic())){
			String imgs = jiuObject2.getPic();
				//1919图片Url特殊处理
				if(jiuObject2.getWebCode() != null && jiuObject2.getWebCode().equals("2")){
					String[] imgAll = jiuObject2.getPic().split("\\?|,");
					for(String tmp : imgAll){
						if(StringUtils.isNotEmpty(tmp) && tmp.startsWith("http")){
							imgList.add(tmp);
						}
					}

				}else{
					String[] imgAll = imgs.split(",");
					for(String tmp: imgAll){
						if(StringUtils.isNotEmpty(tmp) && tmp.startsWith("http:")  && tmp.endsWith("jpg")){
							imgList.add(tmp);
						}
						
					}
				}
				map.put("imgList", imgList);
		}
		//获取评论
		List<CommentObject> commentList = productService.getCommentByGoodsId(jiuObject);
		map.put("commentList", commentList);
		
		//输出
		try {
			response.setContentType("application/jsonp;charset=utf-8");
			PrintWriter printWriter = response.getWriter();
			ObjectMapper objectMapper = new ObjectMapper();
			String result = objectMapper.writeValueAsString(map);
			String callback = request.getParameter("callback");
			printWriter.print(callback+"("+result+")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("recentList")
	public void recentList(HttpServletRequest request, HttpServletResponse response){
		String idString = request.getParameter("ids");
		String[] ids= idString.split(",");
		List<String> idList = Arrays.asList(ids);
		List<JiuObject> jiuObjects = productService.findRecentHistory(idList);
		for(JiuObject jiuObject : jiuObjects){
			if(StringUtils.isNotEmpty(jiuObject.getPic())){
				//1919图片Url特殊处理
				if(jiuObject.getWebCode() != null && jiuObject.getWebCode().equals("2")){
					jiuObject.setPic(jiuObject.getPic().split("\\?")[0]);
					continue;
				}
				jiuObject.setPic(jiuObject.getPic().split(",")[0]);
			}
		}
		//输出
		try {
			response.setContentType("application/jsonp;charset=utf-8");
			PrintWriter printWriter = response.getWriter();
			ObjectMapper objectMapper = new ObjectMapper();
			String result = objectMapper.writeValueAsString(jiuObjects);
			String callback = request.getParameter("callback");
			printWriter.print(callback+"("+result+")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
